import { message } from "@/utils/message";
import { queryProducePaperAllList } from "@/api/exam";
import { reactive, ref, onMounted } from "vue";
import { type PaginationProps } from "@pureadmin/table";

export function useForm() {
  const paperList = ref([]);
  const loading = ref(true);

  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });

  const columns: TableColumnList = [
    {
      label: "试卷名称",
      prop: "name",
      minWidth: 140
    },
    {
      label: "试卷类型",
      prop: "type",
      minWidth: 80,
      formatter: row => {
        switch (row.type) {
          case 0:
            return "固定题型";
          case 1:
            return "随机题型";
        }
      }
    },
    {
      label: "考试时长",
      prop: "times",
      minWidth: 80,
      formatter: row => {
        return `${row.times}(分钟)`;
      }
    },
    {
      label: "试卷分数",
      prop: "score",
      minWidth: 80,
      formatter: row => {
        return `${row.score}分`;
      }
    },
    {
      label: "创建时间",
      prop: "addtime",
      minWidth: 120
    },
    {
      label: "操作",
      fixed: "right",
      width: 80,
      slot: "operation"
    }
  ];

  async function onSearch() {
    queryProducePaperAllList({
      pageSize: pagination.pageSize,
      currentPage: pagination.currentPage
    }).then(res => {
      if (res.success) {
        console.log(JSON.stringify(res));
        paperList.value = res.data.list;
        pagination.total = res.data.total;
        loading.value = false;
      } else {
        message("查询数据异常:" + res.errMsg, { type: "error" });
      }
    });
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  onMounted(() => {
    onSearch();
  });

  return {
    loading,
    columns,
    paperList,
    pagination,
    onSearch,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
